﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class UserAdd : System.Web.UI.Page
    {
        protected string id;
        protected string pid;
        protected User user;
        protected string resultMessage = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"];
            pid = Request.QueryString["pid"];

            if (!IsPostBack)
            {
                this.lblTitle.Text = "新增用户";
                setControls();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            add();

            this.lbResult.Text = resultMessage;
        }

        private void add()
        {
            user = new User()
            {
                Id = Guid.NewGuid(),
            };
            getFields();

            Users_Dao dao = new Users_Dao();

            try
            {
                int result = dao.Add(pid, user);
                if (result >= 1)
                {
                    resultMessage = "添加用户成功";
                }
            }
            catch (Exception ex)
            {
                resultMessage = "添加用户失败:" + ex.Message;
            }
        }

        private void setFields()
        {
            this.tbCode.Text = user.Code.Trim();
            this.tbName.Text = user.Name;
            this.rbGender.SelectedIndex = (int)user.Gender;
            this.tbPassword.Text = user.Password;
            this.tbBirthday.Text = user.Birthday.ToString();
            this.tbTel.Text = user.Tel;
            this.tbMobile.Text = user.Mobile;
            this.tbEmail.Text = user.Email;
            this.tbAddress.Text = user.Address;
        }

        private void getFields()
        {
            user.Code = this.tbCode.Text.Trim();
            user.Name = this.tbName.Text.Trim();
            user.Gender = int.Parse(this.rbGender.SelectedValue);
            user.Password = this.tbPassword.Text;
            string dtstr = this.tbBirthday.Text.Trim();
            if (!string.IsNullOrEmpty(dtstr))
            {
                user.Birthday = DateTime.Parse(dtstr);
            }
            user.Tel = this.tbTel.Text.Trim();
            user.Mobile = this.tbMobile.Text.Trim();
            user.Email = this.tbEmail.Text.Trim();
            user.Address = this.tbAddress.Text.Trim();
        }

        private void setControls()
        {
            this.tbBirthday.Attributes.Add("onclick", "show()");
        }

        private void ClearControlsValue()
        {
            this.tbCode.Text = "";
            this.tbName.Text = "";
            this.rbGender.SelectedIndex = 1;
            this.tbPassword.Text = "";
            this.tbBirthday.Text = "";
            this.tbTel.Text = "";
            this.tbMobile.Text = "";
            this.tbEmail.Text = "";
            this.tbAddress.Text = "";

            this.lbResult.Text = "";
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearControlsValue();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            AsyncPostBackTrigger outsideTrigger = new AsyncPostBackTrigger();
            outsideTrigger.ControlID = "btnSave";
            outsideTrigger.EventName = "Click";
            this.UpdatePanel2.Triggers.Add(outsideTrigger);
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            ClearControlsValue();
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("UserManage.aspx");
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            add();

            this.lbResult.Text = resultMessage;
        }
    }
}